package com.yxw.live_vod_backend_boot.controller;


import com.yxw.live_vod_backend_boot.dto.menu.MenuAddDTO;
import com.yxw.live_vod_backend_boot.dto.menu.MenuQueryDTO;
import com.yxw.live_vod_backend_boot.dto.menu.MenuUpdateDTO;
import com.yxw.live_vod_backend_boot.entity.YxCsMenuEntity;
import com.yxw.live_vod_backend_boot.service.MenuService;
import com.yxw.live_vod_backend_boot.util.UserUtil;
import com.yxw.live_vod_backend_boot.vo.YxCsMenuVo;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fangmaoxin
 * @version 1.0
 * @date 2023年9月14日
 * 客服工作设置
 */
@RestController
@Api(value = "菜单管理", tags = "菜单管理")
@Slf4j
@RequestMapping("/menu/v1")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 查询系统菜单列表
     *
     * @return 菜单列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询系统菜单列表")
    public BaseResult list(@RequestBody MenuQueryDTO dto) {
        List<YxCsMenuVo> menuList = menuService.selectMenuList(dto);
        return BaseResult.doSuccess(menuList,"查询成功！");
    }

    /**
     * 菜单树列表
     *
     * @return 菜单列表
     */
    @PostMapping("/menuTree")
    @ApiOperation(value = "菜单树列表")
    public BaseResult menuTree(@RequestBody MenuQueryDTO dto) {
        List<YxCsMenuVo> menuList = menuService.menuTree(dto);
        return BaseResult.doSuccess(menuList,"查询成功！");
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/getMenuInfoByMenuId/{menuId}")
    @ApiOperation(value = "根据菜单编号menuId 获取详细信息")
    public BaseResult getInfo(@PathVariable Long menuId) {
        return BaseResult.doSuccess(menuService.selectMenuById(menuId),"查询成功！");
    }

    /**
     * 获取菜单下拉树列表
     */
    @PostMapping("/treeSelect")
    @ApiOperation(value = "获取菜单下拉树列表")
    public BaseResult treeSelect(@RequestBody MenuQueryDTO dto) {
        List<YxCsMenuVo> menus = menuService.selectMenuList(dto);
        return BaseResult.doSuccess(menuService.buildMenuTreeSelect(menus),"查询成功！");
    }

    /**
     * 用户登录之后加载他所对应角色的菜单列表树
     */
    @PostMapping("/getMenuTreeByUserRole/{userId}")
    @ApiOperation(value = "用户登录之后加载他所对应角色的菜单列表树")
    public BaseResult getMenuTreeByUserRole(@PathVariable Long userId, @RequestBody MenuQueryDTO dto) {
//        List<YxCsMenuVo> menuList = menuService.getMenuTreeByUserRole(userUtil.getLoginUserId(),dto);
        List<YxCsMenuVo> menuList = menuService.getMenuTreeByUserRole(userId,dto);
        return BaseResult.doSuccess(menuList,"查询成功！");
    }


    /**
     * 新增菜单
     */
    @PostMapping("/addMenu")
    @ApiOperation(value = "新增菜单")
    public BaseResult add(@Validated @RequestBody MenuAddDTO dto) {
        if (!menuService.checkMenuNameUnique(dto)) {
            return BaseResult.failed("新增菜单'" + dto.getMenuName() + "'失败，菜单名称已存在");
        }
        if (!menuService.checkMenuOrderNumUnique(dto)){
            return BaseResult.failed("新增菜单'" + dto.getMenuName() + "'失败，同级菜单排序序号不能重复");
        }
        YxCsMenuEntity entity = new YxCsMenuEntity();
        BeanUtils.copyProperties(dto,entity);
        return BaseResult.doSuccess(menuService.insertMenu(entity),"菜单已成功添加！");
    }

    /**
     * 修改菜单
     */
    @PostMapping("/updateMenuById")
    @ApiOperation(value = "修改菜单")
    public BaseResult edit(@Validated @RequestBody MenuUpdateDTO dto) {
        if (!menuService.checkMenuNameUnique(dto)) {
            return BaseResult.failed("修改菜单'" + dto.getMenuName() + "'失败，菜单名称已存在");
        } else if (dto.getMenuId().equals(dto.getParentId())) {
            return BaseResult.failed("修改菜单'" + dto.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        if (!menuService.checkMenuOrderNumUnique(dto)){
            return BaseResult.failed("修改菜单'" + dto.getMenuName() + "'失败，同级菜单排序序号不能重复");
        }
        YxCsMenuEntity entity = new YxCsMenuEntity();
        BeanUtils.copyProperties(dto,entity);
        return BaseResult.doSuccess(menuService.updateMenu(entity),"菜单已成功修改！");
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/deleteMenuById/{menuId}")
    @ApiOperation(value = "删除菜单")
    public BaseResult remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return BaseResult.failed("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return BaseResult.failed("菜单已分配,不允许删除");
        }
        return BaseResult.doSuccess(menuService.deleteMenuById(menuId),"菜单已成功删除！");
    }
}
